package com.quyeying.utils.wx;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class WxPayConstans {

    // 你的商户私钥
    public static String PRIATE_KEY;

    public static String NOTIFY_URL;

    public static String REFUND_NOTIFY_URL;

    public static String MCH_ID;

    public static String MCH_SERIAL_NO;

    public static String API_V3KEY;

    public static String APP_ID;

    private String appid;
    private String privateKey;
    private String notifyurl;
    private String refundNotifyurl;
    private String mchid;
    private String mchSerialNo;
    private String apiv3;

    @Value("${wx.refundNotifyurl}")
    public void setRefundNotifyurl(String refundNotifyurl) {
        WxPayConstans.REFUND_NOTIFY_URL = refundNotifyurl;
    }


    @Value("${wx.privateKey}")
    public void setPrivateKey(String privateKey) {
        try {
            InputStream certStream = getClass().getClassLoader().getResourceAsStream(privateKey);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024*4];
            int n = 0;
            while (-1 != (n = certStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
            WxPayConstans.PRIATE_KEY = output.toString("UTF8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Value("${wx.notifyurl}")
    public void setNotifyurl(String notifyurl) {
        WxPayConstans.NOTIFY_URL = notifyurl;
    }

    @Value("${wx.mchid}")
    public void setMchid(String mchid) {
        WxPayConstans.MCH_ID = mchid;
    }

    @Value("${wx.mchSerialNo}")
    public void setMchSerialNo(String mchSerialNo) {
        WxPayConstans.MCH_SERIAL_NO = mchSerialNo;
    }

    @Value("${wx.apiv3}")
    public void setApiv3(String apiv3) {
        WxPayConstans.API_V3KEY = apiv3;
    }

    @Value("${wx.appid}")
    public void setAppid(String appid) {
        WxPayConstans.APP_ID = appid;
    }
}
